package libs.yzx.zaza;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

/**
 * Created by Administrator on 2016/7/14
 */
public class AnimActivity extends AppCompatActivity {

    private View[] toAnimViews;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anim);

        toAnimViews = new View[]{
                findViewById(R.id.v1),
                findViewById(R.id.v2),
                findViewById(R.id.v3),
        };


        for (View view : toAnimViews)
            view.setAlpha(0f);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();

    }

    @Override
    public void onEnterAnimationComplete() {
        super.onEnterAnimationComplete();
        animOnEnter();
    }

    public void exit(View view) {
        animOnExit();
    }


    //
    //


    private void animOnEnter() {
        getWindow().getDecorView().postDelayed(new Runnable() {
            public void run() {
                for (int i = 0; i < toAnimViews.length; i++) {
                    final View view = toAnimViews[i];
                    final int ii = i;
                    view.post(new Runnable() {
                        @Override
                        public void run() {
                            int duration1 = 150;
                            int duration2 = 200;
                            int addedDelay = ii * 50;

                            int[] poi = new int[2];
                            view.getLocationOnScreen(poi);
                            PropertyValuesHolder step1 = PropertyValuesHolder.ofFloat("translationY", -poi[1] - view.getHeight(), 200);
                            ObjectAnimator oa1 = ObjectAnimator.ofPropertyValuesHolder(view, step1);
                            oa1.setDuration(duration1);
                            oa1.setInterpolator(new DecelerateInterpolator());
                            oa1.setStartDelay(addedDelay);
                            oa1.addListener(new Animator.AnimatorListener() {
                                public void onAnimationStart(Animator animator) {
                                    view.setAlpha(1f);
                                }

                                public void onAnimationEnd(Animator animator) {
                                }

                                public void onAnimationCancel(Animator animator) {
                                }

                                public void onAnimationRepeat(Animator animator) {
                                }
                            });
                            oa1.start();


                            PropertyValuesHolder step2 = PropertyValuesHolder.ofFloat("translationY", 200, 0);
                            ObjectAnimator oa2 = ObjectAnimator.ofPropertyValuesHolder(view, step2);
                            oa2.setDuration(duration2);
                            oa2.setStartDelay(duration1 + addedDelay);
                            oa2.setInterpolator(new AccelerateInterpolator());
                            oa2.start();
                        }
                    });
                }
            }
        }, 0);
    }


    private void animOnExit() {
        final int sh = getResources().getDisplayMetrics().heightPixels;
        for (int i = 0; i < toAnimViews.length; i++) {
            final View view = toAnimViews[i];
            final int ii = i;
            view.post(new Runnable() {
                @Override
                public void run() {
                    int duration1 = 200;
                    int duration2 = 170;
                    int addedDelay = ii * 50;

                    PropertyValuesHolder step1 = PropertyValuesHolder.ofFloat("translationY", 0, -200);
                    ObjectAnimator oa1 = ObjectAnimator.ofPropertyValuesHolder(view, step1);
                    oa1.setDuration(duration1);
                    oa1.setInterpolator(new DecelerateInterpolator());
                    oa1.setStartDelay(addedDelay);
                    oa1.start();

                    int[] poi = new int[2];
                    view.getLocationOnScreen(poi);
                    PropertyValuesHolder step2 = PropertyValuesHolder.ofFloat("translationY", -200, sh - poi[1] + 200);
                    ObjectAnimator oa2 = ObjectAnimator.ofPropertyValuesHolder(view, step2);
                    oa2.setDuration(duration2);
                    oa2.setInterpolator(new AccelerateInterpolator());
                    oa2.setStartDelay(duration1 + addedDelay);
                    oa2.start();
                }
            });
        }

        new Handler().postDelayed(new Runnable() {
            public void run() {
                finish();
            }
        }, 400);
    }

}
